package com.huawei.fakedouyin.appui;

import static com.huawei.fakedouyin.util.UtilKt.showToast;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.huawei.fakedouyin.R;
import com.huawei.fakedouyin.base.BaseActivity;
import com.huawei.fakedouyin.databinding.ActivityLoginBinding;
import com.huawei.fakedouyin.service.SocketService;
import com.huawei.fakedouyin.util.ActivityUtil;
import com.huawei.fakedouyin.util.LiveDataSet;
import com.huawei.fakedouyin.util.MainExecutor;
import com.huawei.fakedouyin.util.SPUtil;
import com.huawei.faketaobao.constant.IConstant;
import com.huawei.faketaobao.constant.SPConstant;
import com.huawei.faketaobao.http.BaseResult;
import com.huawei.faketaobao.http.FakeRepository;

import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext;

/**
 * 登陆界面
 */
public class LoginActivity extends BaseActivity {

    ActivityLoginBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //修改状态栏颜色
        getWindow().setStatusBarColor(Color.WHITE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initClick();
        binding.loginAccount.setText(SPUtil.Companion.getInstance().getString(SPConstant.UserAccount, "user106"));
        binding.loginPwd.setText(SPUtil.Companion.getInstance().getString(SPConstant.UserPwd, "123456"));
        //socket连接成功的监听
        LiveDataSet.SocketConnectedStatus.observeForever(MainExecutor.get(), observer);

    }

    private void initClick() {
        //登陆
        binding.loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //是否第一次连接socket
                SocketService.isFirstConnectSocket = true;
                //网络请求 登陆接口
                new FakeRepository().postLogin(binding.loginAccount.getText().toString(), binding.loginPwd.getText().toString(), new Continuation<BaseResult<Object>>() {
                    @NonNull
                    @Override
                    public CoroutineContext getContext() {
                        return EmptyCoroutineContext.INSTANCE;
                    }

                    @Override
                    public void resumeWith(@NonNull Object o) {
                        BaseResult result = (BaseResult) o;
                        showToast(result.getMsg());
                        //登陆成功
                        if (result.getCode() == IConstant.CodeSuccess) {
                            //存储登陆账号 连接socket socket连接成功后跳转主页面
                            SPUtil.Companion.getInstance().put(SPConstant.UserAccount, binding.loginAccount.getText().toString());
                            SPUtil.Companion.getInstance().put(SPConstant.UserPwd, binding.loginPwd.getText().toString());
                            SocketService.startConnectSocket(ActivityUtil.Companion.getInstance().getCurrentActivity());
                        }
                    }
                });
            }
        });

        //关闭按钮
        binding.close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //账号输入框监听
        binding.loginAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //更改登陆按钮颜色状态
                if (!charSequence.toString().isEmpty() && !binding.loginPwd.getText().toString().isEmpty()) {
                    binding.loginBt.setEnabled(true);
                    binding.loginBt.setBackground(getDrawable(R.drawable.btn));
                } else {
                    binding.loginBt.setEnabled(false);
                    binding.loginBt.setBackground(getDrawable(R.drawable.btn2));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //密码框输入监听
        binding.loginPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //更新登陆按钮颜色状态
                if (!charSequence.toString().isEmpty() && !binding.loginAccount.getText().toString().isEmpty()) {
                    binding.loginBt.setEnabled(true);
                    binding.loginBt.setBackground(getDrawable(R.drawable.btn));
                } else {
                    binding.loginBt.setEnabled(false);
                    binding.loginBt.setBackground(getDrawable(R.drawable.btn2));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    /**
     * socket连接监听
     */
    private final Observer<Boolean> observer = n -> {
        {
            if (n) {
                //socket连接成功后 进入主页面
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            } else {
                showToast("socket连接失败 请重试");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //移除监听
        LiveDataSet.SocketConnectedStatus.removeObserver(observer);
    }
}
